package net.socket.server.message;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import advice.ExceptionManager;
import io.netty.buffer.ByteBuf;
import net.socket.server.message.base.IMessageBody;
import net.socket.server.message.base.MessageBody;
import net.socket.server.message.base.MessageHeader;

import net.socket.server.protocal.PROTOID;
import net.socket.server.request.CancelCollectSingerRequest;
import net.socket.server.request.CancelCollectSongRequest;
import net.socket.server.request.ChallengePlayerRequest;
import net.socket.server.request.ChangeAvatarRequest;
import net.socket.server.request.CollectSingerRequest;
import net.socket.server.request.CollectSongRequest;
import net.socket.server.request.EnrollRequest;

import net.socket.server.request.GMOrderRequest;
import net.socket.server.request.GetCollectInfoRequest;
import net.socket.server.request.GetEnrollInfoRequest;
import net.socket.server.request.GetHistorySongListRequest;
import net.socket.server.request.GetMatchInfoRequest;
import net.socket.server.request.GetRandomEnrollListRequest;
import net.socket.server.request.GetRankListRequest;
import net.socket.server.request.IRequest;
import net.socket.server.request.KCoinRechargeRequest;
import net.socket.server.request.LeaveFightRequest;
import net.socket.server.request.LocalPlayResultRequest;
import net.socket.server.request.MachineLoginRequest;
import net.socket.server.request.MachineTimeVerificationRequest;
import net.socket.server.request.MatchChoiceRequest;
import net.socket.server.request.P2PConnectionReadyRequest;
import net.socket.server.request.PayQRCodeRequest;
import net.socket.server.request.PlayerLogoutRequest;
import net.socket.server.request.QuitEnrollRequest;
import net.socket.server.request.UploadRecordRequest;
import net.socket.server.request.testLogin;
import util.MyFactory;

public class MessageManager {
	private static Logger logger = Logger.getLogger(ExceptionManager.class);
	
	public ByteBuf generateCommandBuf(int cmd, int machineID, Object bodyMessage) throws IllegalArgumentException, IllegalAccessException, JsonProcessingException{
		
		MessageHeader header = new MessageHeader();
        header.setCommand(cmd);
		header.setMachineID(machineID);
		MessageBody body = new MessageBody();
		body.setJsonStringByObject(bodyMessage);
//		if(cmd!=PROTOID.REQ_TRANSPOND_SOCRE)
//			logger.info("cmd:  "+cmd+"  machineID:  "+machineID+"\n"+"Message:"+body.getJsonString());
		ByteBuf buf1 = body.encode();
		header.setLength(header.getClassLength() + buf1.readableBytes());
		ByteBuf buf2 = header.encode();
		buf2.writeBytes(buf1);
		return buf2;
	}
	
	public IMessageBody chooseMessageBody(MessageHeader header){
		return MyFactory.context.getBean("MessageBody", MessageBody.class);
	}
	
	public IRequest translateToRequest(MessageHeader header, IMessageBody body) throws JsonParseException, JsonMappingException, IOException{
		IRequest request = null;
		//System.out.println("getCommand---"+header.getCommand());
		System.out.println("GET BODY----------"+((MessageBody)body).getJsonString());
//		if(header.getCommand()!=PROTOID.REQ_TRANSPOND_SOCRE)
//			logger.info("client :  "+((MessageBody)body).getJsonString());
		ObjectMapper mapper = new ObjectMapper();
		if(header.getCommand() == PROTOID.LOGIN_GAME_SVR_REQ){
			
			request = mapper.readValue(((MessageBody)body).getJsonString(), MachineLoginRequest.class);
		}else if(header.getCommand() == PROTOID.MACHINE_TIME_VERIFICATION_REQ) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), MachineTimeVerificationRequest.class);
		}
//		else if(header.getCommand() == PROTOID.PLAYER_LOGOUT_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), PlayerLogoutRequest.class);
//		}else if(header.getCommand() == PROTOID.MATCH_ENROLL_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), EnrollRequest.class);
//		}else if(header.getCommand() == PROTOID.MATCH_QUIT_ENROLL_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), QuitEnrollRequest.class);
//		}else if(header.getCommand() == PROTOID.MATCH_CHOICE_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), MatchChoiceRequest.class);
//		}else if(header.getCommand() == PROTOID.P2P_CONNECTION_READY_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), P2PConnectionReadyRequest.class);
//		}
		//else if(header.getCommand() == PROTOID.FIGHT_FINISH_REQ){
		//	request = mapper.readValue(((MessageBody)body).getJsonString(), FightFinishRequest.class);
		//}
//		else if(header.getCommand() == PROTOID.LEAVE_FIGHT_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), LeaveFightRequest.class);
//		}else if(header.getCommand() == PROTOID.GET_RANDOM_ENROLL_LIST_REQ){
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetRandomEnrollListRequest.class);
//		}else if(header.getCommand() == PROTOID.CHANGE_AVATAR_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), ChangeAvatarRequest.class);	
//		}else if (header.getCommand() == PROTOID.CHALLENGE_PLAYER_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), ChallengePlayerRequest.class);
//		}else if (header.getCommand() == PROTOID.GET_ENROLL_INFO_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetEnrollInfoRequest.class);
//		}else if (header.getCommand() == PROTOID.GET_MATCH_INFO_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetMatchInfoRequest.class);
//		}else if (header.getCommand() == PROTOID.COLLECT_SONG_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), CollectSongRequest.class);
//		}else if (header.getCommand() == PROTOID.COLLECT_SINGER_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), CollectSingerRequest.class);
//		}
		else if (header.getCommand() == PROTOID.LOCAL_PLAY_RESULT_REQ) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), LocalPlayResultRequest.class);
		}
		else if (header.getCommand() == PROTOID.END_PLAY_RSP) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), LocalPlayResultRequest.class);
		}
//	else if (header.getCommand() == PROTOID.GET_HISTORY_SONG_LIST_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetHistorySongListRequest.class);
//		}else if (header.getCommand() == PROTOID.GET_COLLECT_INFO_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetCollectInfoRequest.class);
//		}else if (header.getCommand() == PROTOID.GET_RANK_LIST_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), GetRankListRequest.class);
//		}else if (header.getCommand() == PROTOID.CANCEL_COLLECT_SONG_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), CancelCollectSongRequest.class);
//		}else if (header.getCommand() == PROTOID.CANCEL_COLLECT_SINGER_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), CancelCollectSingerRequest.class);
//		}else if (header.getCommand() == PROTOID.PAY_QRCODE_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), PayQRCodeRequest.class);
//		}else if (header.getCommand() == PROTOID.REQ_MID_TRADR_PACKAGE) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), IRequest.class);
//		}
		else if (header.getCommand() == PROTOID.UPLOAD_RECORD_REQ) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), UploadRecordRequest.class);
		}
//		else if (header.getCommand() == PROTOID.KCOIN_RECHARGE_REQ) {
//			request = mapper.readValue(((MessageBody)body).getJsonString(), KCoinRechargeRequest.class);
//		}
		else if (header.getCommand() == PROTOID.REQ_GM_ORDER) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), GMOrderRequest.class);
		}
		//else if (header.getCommand() == PROTOID.REQ_TRANSPOND_SOCRE) {
		//	request = mapper.readValue(((MessageBody)body).getJsonString(), TranspondSore.class);
		//}
		else if (header.getCommand() == PROTOID.TEST_LOGIN) {
			request = mapper.readValue(((MessageBody)body).getJsonString(), testLogin.class);
		}
		else{
			// to be added in the future
			System.out.println("inthefuture");
		}
		if(request != null)
			request.setMachineId(header.getMachineID());
		return request;
	}
	
	/*
	public ChannelFuture sendCommand(ChannelHandlerContext ctx, MessageHeader header, IMessageBody body) throws IllegalArgumentException, IllegalAccessException{
		ChannelFuture cf = null;
		ByteBuf buf1 = body.encode();
		header.setLength(header.getClassLength() + buf1.readableBytes());
		ByteBuf buf2 = header.encode();
		buf2.writeBytes(buf1);
		while (true) {
			if(ctx.channel().isWritable()){
				cf = ctx.writeAndFlush(buf2);
				break;
			}
			
		}
		return cf;
	}*/
	/*
	public ChannelFuture sendCommandSimple(ChannelHandlerContext ctx, int cmd, int machineID, ICommand body) throws IllegalArgumentException, IllegalAccessException{
		ChannelFuture cf = null;
		CommandHeader header = new CommandHeader();
        header.setCommand(cmd);
		header.setMachineID(machineID);
		ByteBuf buf1 = body.encode();
		header.setLength(header.getClassLength() + buf1.readableBytes());
		ByteBuf buf2 = header.encode();
		buf2.writeBytes(buf1);
		while (true) {
			if(ctx.channel().isWritable()){
				cf = ctx.writeAndFlush(buf2);
				break;
			}
		}
		return cf;
	}
	*/
	
	
}
